<template>
  <view class="box_11" v-if="listv.data">
    <view
      class="box_item"
      v-for="(item, index) in listv.data"
      :key="index"
      @click="toDetail(item)"
    >
      <view
        v-show="!flag"
        class="item_left_clickView"
        @click.stop="change(item)"
      >
        <image
          v-if="!item.clickable"
          class="item_left_click"
          src="../static/images/my/icon_grzy_quanxuan.png"
          mode=""
        ></image>
        <image
          v-else
          class="item_left_clickable"
          src="../static/images/my/icon_grzy_xc_xuanzhong.png"
          mode=""
        ></image>
      </view>
      <view class="box_left">
        <image :src="item.img_avatar_url" mode=""></image>
      </view>
      <view
        class="box_right"
        :style="`border-bottom:${
          isWhite
            ? 'border-bottom: 1rpx solid rgba(255,255,255,0.3);'
            : 'border-bottom: 1rpx solid #eee;'
        }`"
      >
        <view class="title" :style="`color:${isWhite ? '#EEEEEE' : '#444444'}`">
          {{ item.name }}
        </view>
        <view
          class="content_text"
          :style="`color:${isWhite ? '#ADADAD' : '#888888'}`"
        >
          {{ item.people_total }}成员
        </view>

        <view class="actions" v-if="type" @click.stop="actions(item.id)">
          <u-icon name="more-dot-fill" color="#eee" size="18"></u-icon>
        </view>
      </view>
    </view>
    <view :style="{width:'100%',height:'50rpx'}"></view>
    <u-action-sheet
      :actions="list4"
      :closeOnClickOverlay="true"
      :closeOnClickAction="true"
      :show="show2"
      @close="show2 = false"
      @select="seleClick"
      cancelText="取消"
    ></u-action-sheet>

    <view class="bottom" v-show="!flag">
      <view class="bottom_left">
        <view @click="changeClick">
          <image
            v-if="!clickableAll"
            class="item_left_click"
            src="../static/images/my/icon_grzy_quanxuan.png"
            mode=""
          ></image>
          <image
            v-else
            class="item_left_click"
            src="../static/images/my/icon_grzy_xc_xuanzhong.png"
            mode=""
          ></image>
        </view>
        <view class="bottom_left_text">全选</view>
      </view>
      <view class="bottom_right" @click="deleteFn">删除</view>
    </view>

    <u-modal
      :show="show"
      :title="title"
      :content="content"
      confirmColor="#55B877"
      :closeOnClickOverlay="true"
      @confirm="confirm"
      @close="close"
      @cancel="cancel"
      :showCancelButton="true"
    ></u-modal>

    <u-modal
      :show="show3"
      :title="title"
      :content="content"
      confirmColor="#55B877"
      :closeOnClickOverlay="true"
      @confirm="confirm3"
      @close="show3 = false"
      @cancel="show3 = false"
      :showCancelButton="true"
    ></u-modal>

    <view class="empty_view" v-if="listv.data && listv.data.length == 0">
      <image
        class="item_left_clickable"
        src="../static/images/my/kong.png"
        mode=""
      ></image>
      <view v-if="from == 'history'">您还没有历史记录~</view>
      <view v-else>当前没有相关数据，请查看其他分类或功能。</view>
    </view>
  </view>
</template>

<script>
export default {
  props: {
    isWhite: {
      type: Boolean,
      default: false,
    },
    flag: {
      type: Boolean,
      default: true,
    },
    type: {
      type: String,
      default: "",
    },
    list: {
      type: Object,
      default: () => {
        return {};
      },
    },
    from: {
      type: String,
      default: "",
    },
  },
  watch: {
    list: {
      deep: true, // 深度监听每一个属性值, 如果是对象可以深度监听
      immediate: true, // 进入页面后立即自动调用handler函数
      handler(newValue) {
        this.listv = newValue;
		// console.log('this.listv.data',this.listv.data);
        if (this.listv.data) {
          for (let i = 0; i < this.listv.data.length; i++) {
            this.listv.data[i].clickable = false;
          }
        }

        // console.log(this.listv, "newList");
      },
    },
  },
  data() {
    return {
      show2: false,
      show3: false,
      title: "确认退出",
      content: "是否确认退出该星球",
      list4: [
        {
          name: "退出星球",
          color: "#EE6060",
        },
      ],
      listv: [],
      clickableAll: false,
      show: false,
      title: "确认删除",
      content: "确认删除选中内容？",
      textVal: "",
      id: "",
    };
  },
  mounted() {
    if (this.type == "myFollow") {
      this.list4 = [
        {
          name: "取消关注",
          color: "#EE6060",
        },
      ];
    } else if (this.type == "myJoin") {
      this.list4 = [
        {
          name: "退出星球",
          color: "#EE6060",
        },
      ];
    } else if (this.type == "myRelease") {
      this.list4 = [
        {
          name: "转让星球",
        },
        {
          name: "解散星球",
          color: "#EE6060",
        },
      ];
    }
  },
  methods: {
    toDetail(item) {
      uni.navigateTo({
        url: "/pages/distinguish/discussionGroup?id=" + item.id,
      });
    },
    change(item) {
      this.$forceUpdate();
      item.clickable = !item.clickable;
      // 判断是否全选
      let all = true;
      for (let i = 0; i < this.listv.data.length; i++) {
        if (this.listv.data[i].clickable == false) {
          all = false;
        }
      }
      if (all) {
        this.clickableAll = true;
      } else {
        this.clickableAll = false;
      }
    },
    actions(id) {
      this.show2 = true;
      // console.log("33333");
      this.id = id;
    },
    confirm3() {
      if (this.textVal == "解散星球") {
        this.title = '确认解散'
        // console.log("解散星球")
        this.$http
          .usergongnengJiesan({ id: this.id })
          .then((res) => {
            // console.log(res, "解散");
            uni.$u.toast(res.message);
            this.show3 = false
            setTimeout(() => {
              this.$emit("updateDataList");
            }, 1000);
          })
          .catch((error) => {
            uni.$u.toast(error);
          });
      } else if(this.textVal == "转让星球"){
        // console.log("转让星球")
        this.show3 = false
      } else if(this.textVal == "退出星球"){
        // console.log("退出星球")
        this.exit(this.id)
        
      } else if(this.textVal == "取消关注"){
        // console.log("取消关注")
        // this.show3 = false
        this.unfollow(this.id)
      }
    },
    changeClick() {
      this.clickableAll = !this.clickableAll;
      if (this.clickableAll) {
        for (let i = 0; i < this.listv.data.length; i++) {
          this.listv.data[i].clickable = true;
        }
      } else {
        for (let i = 0; i < this.listv.data.length; i++) {
          this.listv.data[i].clickable = false;
        }
      }
    },
    confirm() {
      let arr = [];
      let num = 0;
      for (let i = 0; i < this.listv.data.length; i++) {
        if (this.listv.data[i].clickable == true) {
          arr.push(this.listv.data[i].history_id);
          num++;
        }
      }
      // console.log(arr, "数组");
      this.deletFn(arr);
    },
    close() {
      this.show = false;
    },
    cancel() {
      this.show = false;
    },
    // 删除按钮
    deleteFn() {
      let isTrue = true;
      for (let i = 0; i < this.listv.data.length; i++) {
        // console.log(this.listv.data[i].clickable);
        if (this.listv.data[i].clickable == true) {
          isTrue = false;
        }
      }
      if (isTrue == true) {
        uni.$u.toast("您还未选择内容");
      } else {
        this.show = true;
      }
    },
    // 删除接口
    deletFn(arr) {
      this.$http
        .historyDel_his_xinghe({ ids: arr })
        .then((res) => {
          uni.$u.toast(res.message);
          this.show = false;
          setTimeout(() => {
            this.$emit("updateDataXingHe");
          }, 1000);
        })
        .catch((error) => {
          uni.$u.toast(error);
        });
    },
    // 退出星球
    exit(id){
      this.$http
        .xingheJiaRu({ id: id,type:2 })
        .then((res) => {
          uni.$u.toast(res.message);
          this.show3 = false
          setTimeout(() => {
            this.$emit("updateDataJiaRu");
          }, 1000);
        })
        .catch((error) => {
          uni.$u.toast(error);
        });
    },
    // 取消关注
    unfollow(id){
      this.$http
        .xingheCollect({ id: id,type:2 })
        .then((res) => {
          uni.$u.toast(res.message);
          this.show3 = false
          setTimeout(() => {
            this.$emit("updateDataQuGuan");
          }, 1000);
        })
        .catch((error) => {
          uni.$u.toast(error);
        });
    },
    seleClick(e) {
      // console.log(e,11111111111);
      this.textVal = e.name;
      this.title = e.name
      if(e.name == '解散星球'){
        this.content = '是否确认解散该星球'
      } else if(e.name == '转让星球'){
        // this.content = '是否确认转让星球'
        uni.navigateTo({
          url:`/pages/integral/Planet?id=${this.id}`
        })
        return 
      } else if(e.name == '取消关注'){
        this.content = '是否确认取关该星球'
      } else if(e.name == '退出星球'){
        this.content = '是否确认退出该星球'
        
      }
      this.show3 = true;
    },
  },
};
</script>

<style scoped lang="scss">
.box_11 {
  width: 100%;
  height: 100%;
  padding: 0 30rpx 0;
  box-sizing: border-box;
  .box_item {
    width: 100%;
    display: flex;
    margin-bottom: 24rpx;
    margin-top: 24rpx;
    // margin: 40rpx 0 25rpx;
    .box_left {
      width: 124rpx;
      height: 124rpx;
      image {
        width: 100%;
        height: 100%;
        border-radius: 8rpx;
      }
    }
    .box_right {
      flex: 1;
      //   height: 148rpx;
      margin-left: 24rpx;
      //   padding-left: 24rpx;
      // display: flex;
      // flex-direction: column;
      // justify-content: space-between;
      border-bottom: 1rpx solid rgba(255,255,255,0.3);
      position: relative;
      // padding-bottom: 24rpx;
      .title {
        width: 100%;
        font-size: 32rpx;
        font-family: PingFang SC-Medium, PingFang SC;
        font-weight: 400;
        color: #444444;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        overflow: hidden;
        -webkit-line-clamp: 1;
        text-overflow: ellipsis;
      }
      .content_text {
        font-size: 28rpx;
        font-family: PingFang SC-Medium, PingFang SC;
        font-weight: 400;
        color: #888888;
        margin-top: 45rpx;
        margin-bottom: 32rpx;
      }
      .actions {
        position: absolute;
        right: 0;
        top: 42rpx;
      }
    }
    .item_left_clickView {
      margin-right: 24rpx;
      display: flex;
      align-items: center;
      .item_left_click {
        width: 40rpx;
        height: 40rpx;
      }
      .item_left_clickable {
        width: 40rpx;
        height: 40rpx;
      }
    }
  }

  .bottom {
    width: 100%;
    height: 112rpx;
    background: #272727;
    position: fixed;
    bottom: 0;
    left: 0;
    padding: 16rpx 30rpx;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .bottom_left {
      display: flex;
      align-items: center;
      .item_left_click {
        width: 40rpx;
        height: 40rpx;
      }
      .bottom_left_text {
        margin-left: 16rpx;
        color: #eeeeee;
        font-size: 30rpx;
      }
    }

    .bottom_right {
      width: 250rpx;
      height: 80rpx;
      background: #ee6060;
      border-radius: 48rpx;
      text-align: center;
      line-height: 80rpx;
      font-size: 30rpx;
      color: #eeeeee;
    }
  }

  /deep/.u-modal__content__text {
    text-align: center;
  }
  .empty_view {
    margin-top: 100rpx;
    image {
      margin: 0 auto;
      width: 410rpx;
      height: 410rpx;
    }
    view {
      font-size: 30rpx;
      color: #d8d8d8;
      text-align: center;
    }
  }
}
</style>
